﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class VYGRONAV95 : ViewModelBase
    {
        public string year = "", month = "", day = "", date = "";
        #region=========RaisePropertyChanged=============


        /*
        private string m_FQTYEND;
         public string FQTYEND
         {
             get
             {
                 return m_FQTYEND;
             }
             set
             {
                 m_FQTYEND = value;
                 RaisePropertyChanged("FQTYEND");
            
             }
         }
         private string m_FQTYMAT;
         public string FQTYMAT
         {
             get
             {
                 return m_FQTYMAT;
             }
             set
             {
                 m_FQTYMAT = value;
                 RaisePropertyChanged("FQTYMAT");
             }
         }
         private string m_RQTYEND;
         public string RQTYEND
         {
             get
             {
                 return m_RQTYEND;
             }
             set
             {
                 m_RQTYEND = value;
                 RaisePropertyChanged("RQTYEND");
             }
         }
         private string m_RQTYMAT;
         public string RQTYMAT
         {
             get
             {
                 return m_RQTYMAT;
             }
             set
             {
                 m_RQTYMAT = value;
                 RaisePropertyChanged("RQTYMAT");
             }
         }
         private string m_KQTYEND;
         public string KQTYEND
         {
             get
             {
                 return m_KQTYEND;
             }
             set
             {
                 m_KQTYEND = value;
                 RaisePropertyChanged("KQTYEND");
             }
         }
         private string m_KQTYMAT;
         public string KQTYMAT
         {
             get
             {
                 return m_KQTYMAT;
             }
             set
             {
                 m_KQTYMAT = value;
                 RaisePropertyChanged("KQTYMAT");
             }
         }
         private string m_DAB05;
         public string DAB05
         {
             get
             {
                 return m_DAB05;
             }
             set
             {
                 m_DAB05 = value;
                 RaisePropertyChanged("DAB05");
             }
         }
         private string m_DAB55;
         public string DAB55
         {
             get
             {
                 return m_DAB55;
             }
             set
             {
                 m_DAB55 = value;
                 RaisePropertyChanged("DAB55");
             }
         }
         private string m_DAB45;
         public string DAB45
         {
             get
             {
                 return m_DAB45;
             }
             set
             {
                 m_DAB45 = value;
                 RaisePropertyChanged("DAB45");
             }
         }
         private string m_DAB50;
         public string DAB50
         {
             get
             {
                 return m_DAB50;
             }
             set
             {
                 m_DAB50 = value;
                 RaisePropertyChanged("DAB50");
             }
         }*/
        #endregion
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        private DataRow m_dwVKP = null;
        public DataRow dwVKP
        {
            get
            {
                return m_dwVKP;
            }
            set
            {
                m_dwVKP = value;
                KRS = dwVKP["VKPAUFKRS"].ToString();
                LFD = dwVKP["VKPAUFLFD"].ToString();
                POS = dwVKP["VKPAUFPOS"].ToString();
                OnPropertyChanged("dwVKP", value);
                ABPLFD = "X";
            }
        }
        #region=============CollectionViewSource===========
        public SETINTEXDB SetEnv
        {
            get;
            set;
        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
        public SETINTEXDB SetVKP
        {
            get;
            set;
        }
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }

        #endregion
        public string PlanID = "";

        public bool IsMaster { get { return app.IsMaster; } }
        private bool _IsDelete; public bool IsDelete { get { return _IsDelete; } set { _IsDelete = value; OnPropertyChanged("IsDelete", value); } }
        private bool _IsPublish; public bool IsPublish { get { return _IsPublish; } set { _IsPublish = value; OnPropertyChanged("IsPublish", value); } }
        private bool _IsPlan; public bool IsPlan { get { return _IsPlan; } set { _IsPlan = value; OnPropertyChanged("IsPlan", value); } }
        private string _KRS; public string KRS { get { return _KRS; } set { _KRS = value; OnPropertyChanged("KRS", value); } }
        private string _LFD; public string LFD { get { return _LFD; } set { _LFD = value; OnPropertyChanged("LFD", value); } }
        private string _POS; public string POS { get { return _POS; } set { _POS = value; OnPropertyChanged("POS", value); } }
        private double _GSFACH; public double GSFACH { get { return _GSFACH; } set { _GSFACH = value; OnPropertyChanged("GSFACH", value); } }
        private double _GSGARNNR; public double GSGARNNR { get { return _GSGARNNR; } set { _GSGARNNR = value; OnPropertyChanged("GSGARNNR", value); } }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public string ABPLFD = "X";
        int ABPOS = 1;
        public decimal LDAB05 = 1;
        public decimal LDAB55 = 1;
        public decimal LDAB45 = 1;
        public decimal LDAB50 = 1;
        string PRLO = "", PRLB = "", PRLP = "";
        string time;
        int ABAID = 1;
        public DataTable tbABAUFM = null;
        string filter = "";
        DataRow[] rows = null;
        public ITXABPLANData ITXABPLANData;
        VVKPOS VVKPOS = new VVKPOS();
        VICC_STDMACHINE STM;
        VABAUFM VABAUFM = new VABAUFM();
        public VYGRONAV95()
            : base()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            base.SetEnv = new DataSet();
            SetProd = new DataSet();
            SetData = new DataSet();
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }
            SetEnv.Merge(SetMaster);
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day); 
            ITXABPLANData = new ITXABPLANData(); 
        }
        public void LoadEnv()
        {
            try
            {
                DataSet SResult = new DataSet();
                DataSet SReturn = new DataSet();
                DataRow dwData;
                SetEnv.Clear();
                SetEnv.AcceptChanges();
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["APKMANDANT"] = "00";
                SResult = INTEXDBC.QueryAPKOPF(dwData, 1);
                SetEnv.Merge(SResult);
                SResult = INTEXDBC.QueryICC_PRULES(null, 9999);
                SetEnv.Merge(SResult);
                SResult = INTEXDBC.QueryICC_AV95RULES(null, 9999);
                SetEnv.Merge(SResult);
                SResult = INTEXDBC.QueryAPFOLGE(null, 6);
                SetEnv.Merge(SResult);
                SResult = INTEXDBC.QueryAPARTK(null, 9999);
                SetEnv.Merge(SResult);

                dwData["SDSA"] = "0015";
                SetEnv.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0279";
                SetEnv.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LoadData(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            string KRS = "";
            string LFD = "";
            string MANDANT = "";
            DataRow dwData = null;
            if (dwInput == null)
                dwInput = dwVKP;
            dwData = INTEXDB.ICC_QUERYTB.NewRow();
            try
            {
                KRS = dwInput["VKPAUFKRS"].ToString();
                LFD = dwInput["VKPAUFLFD"].ToString();
                MANDANT = dwInput["VKPMANDANT"].ToString();
                SResult = INTEXBL.QueryVKK(MANDANT, KRS, LFD);
                SReturn.Merge(SResult);
              
                SetMaster.Clear();
                SetMaster.Merge(SReturn);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public bool LoadVKPOSData(DataRow dwInput)
        {
            SetVKP = null;
            bool IsCreated = false;
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            string krs = "";
            string lfd = "";
            string pos = "";
            string MANDANT = "";
            DataRow dwData = null;
            if (dwInput == null)
                dwInput = dwVKP;
            dwData = INTEXDB.ICC_QUERYTB.NewRow();
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetEnv.Tables["APFOLGE"]);
                SetDetail.Merge(SetEnv.Tables["APARTK"]);
                krs = dwInput["VKPAUFKRS"].ToString();
                lfd = dwInput["VKPAUFLFD"].ToString();
                pos = dwInput["VKPAUFPOS"].ToString();
                Dictionary<string, string> list = (Dictionary<string, string>)ITXABPLANData.GETON(krs, lfd, pos);
                dwData["ABPART"] = _KRS + "-" + Helpers.Helper.Right(_LFD, 4).Trim() + "-" + Helpers.Helper.Right(pos, 3).Trim();
                foreach (KeyValuePair<string, string> kp in list)
                {
                    string key = kp.Key;
                    dwData["ABPMANDANT"] = app.Mandant;
                    dwData["ABAMANDANT"] = app.Mandant;
                    dwData["ABPAUFKRS"] = key.Substring(0, 2);
                    dwData["ABPAUFLFD"] = key.Substring(3, 6);
                    dwData["ABPAUFPOS"] = key.Substring(10, 3);
                    dwData["ABAAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABAAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABAAUFPOS"] = dwData["ABPAUFPOS"];
                    dwData["ABAVKAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABAVKAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABAVKAUFPOS"] = dwData["ABPAUFPOS"];
                    SResult = INTEXDBC.QueryICC_ABAX(dwData, 7);
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryABPOS(dwData, 1);
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryABAUFM(dwData, 5);
                    SReturn.Merge(SResult);
                }

                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = krs;
                dwData["VKPAUFLFD"] = lfd;
                dwData["VKPAUFPOS"] = pos;
                SResult = INTEXDBC.QueryGAAUF(dwData, 21);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryGSDAT(dwData, 7);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryGSCOLOR(dwData, 8);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryGASTGA(dwData, 7);
                SReturn.Merge(SResult);
                string yarns = "";
                string Yart = "";
                string Yvar = "";
                DataSet STemp = new System.Data.DataSet();
                bool IsFound = true;
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                while (true)
                {
                    if (!IsFound)
                        break;
                    IsFound = false;
                    if (!SReturn.Tables.Contains("GASTGA"))
                    {
                        break;
                    }
                    foreach (DataRow row in SReturn.Tables["GASTGA"].Rows)
                    {
                        string GAGART = row["GAGGART"].ToString();
                        if (GAGART.StartsWith("1") || GAGART.StartsWith("2"))
                            continue;
                        Yart = row["GAGGART"].ToString();
                        Yvar = row["GAGGVAR"].ToString();
                        if (!yarns.Contains(Yart + Yvar + ";"))
                        {
                            IsFound = true;
                            yarns += Yart + Yvar + ";";
                            break;
                        }
                    }
                    if (IsFound)
                    {
                        dwData["GSMANDANT"] = app.Mandant;
                        dwData["GSART"] = Yart;
                        dwData["GSVAR"] = Yvar;
                        SResult = INTEXDBC.QueryGSDAT(dwData);
                        SReturn.Merge(SResult);

                        dwData["GAGMANDANT"] = app.Mandant;
                        dwData["GAGART"] = Yart;
                        SResult = INTEXDBC.QueryGASTGA(dwData, 2);
                        SReturn.Merge(SResult);
                    }
                }

                #region============组织规格VAD===================
               if (SReturn.Tables.Contains("VKPOS"))
                {

                    foreach (DataRow row in SReturn.Tables["VKPOS"].Rows)
                    {
                        string koll = row["VKPKOLL"].ToString();
                        string art = row["VKPART"].ToString();
                        string color = row["VKPFARBE"].ToString();
                        var query = from DataRow dwColor in SReturn.Tables["GSCOLOR"].AsEnumerable()
                                    where dwColor["GSCART"].ToString().Trim() == art.Trim()
                                    && dwColor["GSCFARBE"].ToString().Trim() == color.Trim()
                                    select dwColor;
                        foreach (DataRow dwColor in query)
                        {
                            row["CONSTRUCTION"] = Helper.GetGB2312Value(dwColor["GSCBEZ"].ToString());
                        }
                    }
                }
                #endregion

                SetDetail.Merge(SReturn);
                SetEnv.Tables["ICC_ABAX"].Clear();
                SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
                tbABAUFM = SetDetail.Tables["ICC_ABAX"].Clone();
                tbABAUFM.TableName = "ICC_ABAX";
                filter = "GAAUFKRS='" + krs + "' AND GAAUFLFD='" + lfd + "' AND GAAUFPOS='" + pos + "'";
                rows = SetDetail.Tables["GSDAT"].Select(filter);
                if (rows.Length == 0)
                {
                    MessageBox.Show("纱线档案不存在");
                }
                foreach (DataRow row in rows)
                {
                    GSFACH = Convert.ToDouble(row["GSFACH"].ToString());
                    GSGARNNR = Math.Round(Convert.ToDouble(row["GSGARNNR"].ToString()) / 10000, 0);
                }
                if (SetDetail.Tables["ICC_ABAX"].Rows.Count > 0)
                {
                    IsCreated = true;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return IsCreated;
        }
        public bool FilterData()
        {
            bool IsCreated = false;
            SetDetail.Clear();
            try
            {
                SetDetail.Merge(SetEnv.Tables["APFOLGE"]);
                SetDetail.Merge(SetEnv.Tables["APARTK"]);
                SetDetail.Merge(SetMaster.Tables["GSDAT"]);
                SetDetail.Merge(SetMaster.Tables["GASTGA"]);
                SetDetail.Merge(SetMaster.Tables["GSCOLOR"]);
                filter = "GAAUFKRS='" + KRS + "' AND GAAUFLFD='" + LFD + "' AND GAAUFPOS='" + POS + "'";
                rows = SetDetail.Tables["GSDAT"].Select(filter);
                if (rows.Length == 0)
                {
                    MessageBox.Show("纱线档案不存在");
                }
                foreach (DataRow row in rows)
                {
                    GSFACH = Convert.ToDouble(row["GSFACH"].ToString());
                    GSGARNNR = Math.Round(Convert.ToDouble(row["GSGARNNR"].ToString()) / 10000, 0);

                    break;
                }

                filter = "EXART LIKE '" + KRS + "-" + Helpers.Helper.Right(LFD, 4) + "-" + Helper.Right(POS, 3) + "%'";
                rows = SetMaster.Tables["ICC_ABAX"].Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_ABAX";
                    SetDetail.Merge(tb);
                    IsCreated = true;
                }
                filter = "VKPAUFKRS='" + KRS + "' AND VKPAUFLFD='" + LFD + "' AND VKPAUFPOS='" + POS + "'";
                rows = SetMaster.Tables["VKPOS"].Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "VKPOS";
                    SetDetail.Merge(tb);
                }
                var query = (from DataRow ABPOS in SetMaster.Tables["ABPOS"].AsEnumerable()
                             from DataRow ABAX in SetMaster.Tables["ICC_ABAX"].AsEnumerable()
                             where ABPOS["ABPAUFKRS"].ToString() == ABAX["ABAAUFKRS"].ToString()
                             && ABPOS["ABPAUFLFD"].ToString() == ABAX["ABAAUFLFD"].ToString()
                             && ABPOS["ABPAUFPOS"].ToString() == ABAX["ABAAUFPOS"].ToString()
                             && ABAX["EXART"].ToString().Trim() == KRS + "-" + Helpers.Helper.Right(LFD, 4) + "-" + Helper.Right(POS, 3)
                             select ABPOS).Distinct();
                if (query.Count() > 0)
                {
                    DataTable tb = query.CopyToDataTable();
                    tb.TableName = "ABPOS";
                    SetDetail.Merge(tb);
                }
                filter = "GAAUFKRS='" + KRS + "' AND GAAUFLFD='" + LFD + "' AND GAAUFPOS='" + POS + "'";
                rows = SetMaster.Tables["GAAUF"].Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "GAAUF";
                    SetDetail.Merge(tb);
                }
                SetEnv.Tables["ICC_ABAX"].Clear();
                SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return IsCreated;

        }
        public void LoadVKPList()
        {
            try
            {
                DataSet SResult = new DataSet();
                SResult = INTEXDBC.QueryICC_PVKPOS(null, 10);
                SResult.Tables[0].TableName = "VKPOS";
                SetEnv.Tables["VKPOS"].Clear();
                SetEnv.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void ReArrangeVKPOS()
        { 
            VVKPOS.DeleteVKPOS(null, 2);
        }
    }
}